﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform target;
    private Camera mainCamera;
    private Transform cameraTrans;
    [SerializeField] private Vector3 cameraOffset;

    [SerializeField] private float rotateSpeed;
    // Start is called before the first frame update
    void Start()
    {
        mainCamera = Camera.main;
        cameraTrans = mainCamera.transform;
        cameraTrans.position = target.position + cameraOffset;
    }

    Vector3 ve;
    private Quaternion angle;
    private void LateUpdate()
    {
        Vector3 target = this.target.position + cameraOffset.y * this.target.up + cameraOffset.z * this.target.forward;
        cameraTrans.position = Vector3.SmoothDamp(cameraTrans.position, target, ref ve, 0);
        angle = Quaternion.LookRotation(this.target.position - target);
        transform.rotation = Quaternion.Slerp(transform.rotation, angle, Time.deltaTime * rotateSpeed);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
